<?php
namespace Test;

use Dybasedev\Dataman\Capsule\Manager as DB;

class ConvertHonor extends Convert
{
    public function convert($school, $schoolId, $groupinfo, $spaceid, $classid, $class, $studentId, $studentOld)
    {
        $search = $this->connection->table('yuanbao_v1.t_student_honor')
            ->where('studentCode', '=', $studentOld->studentCode)
            ->groupBy('grantDate')
            ->lists('grantDate');

        foreach ($search as $grantDate) {
            $t1 = $this->connection->table('yuanbao_v1.t_student_honor')
                ->where('studentCode', '=', $studentOld->studentCode)
                ->where('grantDate', '=', $grantDate)
                ->where('type', '=', 1)
                ->groupBy('type')
                ->select(DB::raw('count(type) as count'))
                ->first();

            $t2 = $this->connection->table('yuanbao_v1.t_student_honor')
                ->where('studentCode', '=', $studentOld->studentCode)
                ->where('grantDate', '=', $grantDate)
                ->where('type', '=', 2)
                ->groupBy('type')
                ->select(DB::raw('count(type) as count'))
                ->first();

            $t3 = $this->connection->table('yuanbao_v1.t_student_honor')
                ->where('studentCode', '=', $studentOld->studentCode)
                ->where('grantDate', '=', $grantDate)
                ->where('type', '=', 3)
                ->groupBy('type')
                ->select(DB::raw('count(type) as count'))
                ->first();

            $t = $this->connection->table('yuanbao_v1.t_student_honor')->where('studentCode', '=', $studentOld->studentCode)
                ->where('grantDate', '=', $grantDate)
                ->first();

            $t1 = is_null($t1) ? 0 : $t1->count; // 桃心 加油
            $t2 = is_null($t2) ? 0 : $t2->count; // 大拇指 表扬
            $t3 = is_null($t3) ? 0 : $t3->count; // 鲜花 鼓励

            $teacher = $this->connection->table('convert.v2_sys_user')->where('user', '=', $t->grantCode)->first();

            if (is_null($teacher)) {
                continue;
            }

            printf("    Select grant date: %s; type count: %d %d %d; Grantor: %s\n", $grantDate, $t1, $t2, $t3, $teacher->user);


            $parent = $this->connection->table('convert.v2_sys_user')->where('user', '=', $t->studentCode)->pluck('id');

            $student = $this->connection->table('convert.v2_school_student')->where('parent_id', '=', $parent)->pluck('id');

            $this->connection->table('convert.v2_school_honor_roll')->insert(array(
                'space' => $spaceid,
                'school_id' => $schoolId,
                'class_id' => $classid,
                'parent_id' => $parent,
                'student_id' => $student,
                'teacher_id' => $teacher->id,
                'praise' => $t2,
                'cheer' => $t1,
                'encouragement' => $t3,
                'created_time' => strtotime($t->publishTime)
            ));
        }
    }
} 